package jin.chen.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jin.chen.pojo.Users;
import jin.chen.pojo.bo.UsersBo;
import jin.chen.service.UserService;
import jin.chen.utils.CookieUtils;
import jin.chen.utils.JsonUtils;
import jin.chen.utils.MD5Utils;
import jin.chen.utils.Result;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.security.provider.MD5;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * userController,处理user相关操作
 */
@Api(value = "用户", tags = "用户注册登录controller")
@RestController
@RequestMapping("/passport")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private Sid sid;

    /**
     * 查询用户是否存在方法
     * @param username  用户名
     * @return
     */
    @ApiOperation(value = "查询用户是否存在接口", notes = "查询用户是否存在接口", httpMethod = "GET")
    @GetMapping("/usernameIsExist")
    public Result queryUserNameIsExist(@RequestParam String username){
        //传入username是否为空
        if(StringUtils.isBlank(username)){
            return Result.errorMsg("用户名不能为空");
        }
        boolean flag = userService.queryUsernameIsExist(username);
        if(flag){
            return Result.errorMsg("用户名已存在");
        }
        return Result.OK();
    }

    @ApiOperation(value = "用户注册接口", notes = "用户注册接口", httpMethod = "POST")
    @PostMapping("/regist")
    public Result regist(@RequestBody UsersBo usersBo){
        String username = usersBo.getUsername();
        String password = usersBo.getPassword();
        String confirmPassword = usersBo.getConfirmPassword();
        //先判空
        if(StringUtils.isBlank(username) || StringUtils.isBlank(password) || StringUtils.isBlank(confirmPassword)){
            return Result.errorMsg("用户或密码为空");
        }
        //密码长度不能小于6位
        if(password.length() < 6){
            return Result.errorMsg("密码长度不能小于6位");
        }
        //输入两次密码要一致
        if(!password.equals(confirmPassword)){
            return Result.errorMsg("两次密码不一致");
        }
        //查询用户是否存在
        boolean flag = userService.queryUsernameIsExist(username);
        if(flag){
            return Result.errorMsg("用户名已存在");
        }
        Users users = userService.createUsers(usersBo);
        return Result.OK(users);
    }

    @ApiOperation(value = "用户登录接口", notes = "用户登录接口", httpMethod = "POST")
    @PostMapping("/login")
    public Result login(@RequestBody UsersBo usersBo, HttpServletRequest request,
                        HttpServletResponse response){
        String username = usersBo.getUsername();
        String password = usersBo.getPassword();
        //先判空
        if(StringUtils.isBlank(username) || StringUtils.isBlank(password) ){
            return Result.errorMsg("用户或密码为空");
        }
        Users users = userService.queryUserForLogin(username, password);
        if(users != null){
            users = setNull(users);
            CookieUtils.setCookie(request, response, "user", JsonUtils.objectToJson(users), true);
            return Result.OK(users);
        }
        return Result.errorMsg("用户名或密码错误");
    }

    @ApiOperation(value = "用户退出登录接口", notes = "用户退出登录接口", httpMethod = "POST")
    @PostMapping("/logout")
    public Result logout(@RequestParam String userId, HttpServletRequest request,
                        HttpServletResponse response){
        CookieUtils.deleteCookie(request, response, "user");
        return Result.OK();

    }

    public Users setNull(Users users){
        users.setBirthday(null);
        users.setUpdatedTime(null);
        users.setCreatedTime(null);
        users.setPassword(null);
        users.setEmail(null);
        users.setRealname(null);
        users.setMobile(null);
        return users;
    }


}
